#!/usr/bin/env python
# coding=utf-8

import MySQLdb,os, sys, re, traceback
import urllib,urllib2
currpath=sys.path[0]
sys.path.append(currpath[:currpath.find('src')+4])
import setting

conn=MySQLdb.Connection(setting.server_vacation,setting.username_vacation,setting.password_vacation,setting.database_vacationdb,use_unicode=True,charset='utf8')

def format_UTF8(html):
    html2=''
    try:
        html2=html.decode('utf-8')
        #html2=unicode(html,'utf-8')
    except:
        try:
            html2=html.decode("GB18030")
        except:
            print traceback.format_exc(sys.exc_info())
    return html2

def delete(url):
    try:
        curs=conn.cursor()
        params=(url,)
        curs.callproc('PROC_OYESGO_VACATIONS_ROUTE_DEL',params)
    except:
        print traceback.format_exc(sys.exc_info())
    finally:
        curs.close()

if __name__ == '__main__':
    curs=conn.cursor()
    try:
        curs.execute("SELECT id,route_url,route_title,price FROM route ORDER BY update_time")
        m=curs.fetchall()
        for r in m:
            try:
                #html=format_UTF8(urllib2.urlopen('http://sz.tuniu.com/tours/85482?p=1026').read())
                html=format_UTF8(urllib2.urlopen(r[1]).read())
                if html[0:40].find('<script>location.href=')>-1:
                    print 'CHANGED NOT FOUND: '+str(r[0])+'\t'+r[1]+'\t'+str(r[3])
                    delete(r[1])
                    continue
                elif html.find(r[2])<0 and html.find(str(r[3]))<0:
                    print 'NOT FOUND: '+str(r[0])+'\t'+r[1]+'\t'+str(r[3])
                    delete(r[1])
                else:
                    print 'OK: '+str(r[0])+'\t'+r[1]+'\t'+str(r[3])
                    continue
            except UnicodeDecodeError,e:
                print html[0:320]
                print '##################################'+str(e)
                print str(r[0])+'\t'+r[1]+'\t'+str(r[3])
            except:
                print traceback.format_exc(sys.exc_info())
                print 'EXCEPT NOT FOUND: '+str(r[0])+'\t'+r[1]+'\t'+str(r[3])
                delete(r[1])
    except:
        print traceback.format_exc(sys.exc_info())
    finally:
        curs.close()
        conn.close()
